package cn.iocoder.yudao.module.trade.delivery.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.region.RegionUtils;
import cn.iocoder.yudao.framework.region.enums.RegionDataSet;
import cn.iocoder.yudao.module.api.infrastructure.user.AdminUserApi;
import cn.iocoder.yudao.module.api.infrastructure.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.api.infrastructure.user.dto.AdminUserSimpleRespDTO;
import cn.iocoder.yudao.module.trade.delivery.dal.dataobject.DeliveryPickUpStoreDO;
import cn.iocoder.yudao.module.trade.delivery.service.DeliveryPickUpStoreService;
import cn.iocoder.yudao.module.trade.delivery.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 自提门店")
@RestController
@RequestMapping("/trade/delivery/pick-up-store")
@Validated
public class DeliveryPickUpStoreController {

    @Resource
    private DeliveryPickUpStoreService deliveryPickUpStoreService;

    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建自提门店")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Long> createDeliveryPickUpStore(@Valid @RequestBody DeliveryPickUpStoreSaveReqVO reqVO) {
        reqVO.setId(null);
        return success(deliveryPickUpStoreService.saveDeliveryPickUpStore(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新自提门店")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> updateDeliveryPickUpStore(@Valid @RequestBody DeliveryPickUpStoreSaveReqVO reqVO) {
        deliveryPickUpStoreService.saveDeliveryPickUpStore(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除自提门店")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> deleteDeliveryPickUpStore(@RequestParam("id") Long id) {
        deliveryPickUpStoreService.deleteDeliveryPickUpStore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得自提门店")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:delivery:pick-up-store:query')")
    public CommonResult<DeliveryPickUpStoreRespVO> getDeliveryPickUpStore(@RequestParam("id") Long id) {
        DeliveryPickUpStoreDO deliveryPickUpStore = deliveryPickUpStoreService.getDeliveryPickUpStore(id);
        if (deliveryPickUpStore == null) {
            return success(null);
        }
        List<AdminUserRespDTO> verifyUsers = new ArrayList<>();
        if (CollUtil.isNotEmpty(deliveryPickUpStore.getVerifyUserIds())) {
            verifyUsers = adminUserApi.getUserList(deliveryPickUpStore.getVerifyUserIds());
        }
        DeliveryPickUpStoreRespVO respVO = BeanUtils.toBean(deliveryPickUpStore, DeliveryPickUpStoreRespVO.class);
        respVO.setVerifyUsers(BeanUtils.toBean(verifyUsers, AdminUserSimpleRespDTO.class));
        return success(respVO);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得自提门店精简信息列表")
    public CommonResult<List<DeliveryPickUpStoreSimpleRespVO>> getSimpleDeliveryPickUpStoreList() {
        List<DeliveryPickUpStoreDO> list = deliveryPickUpStoreService.getDeliveryPickUpStoreListByStatus(
                CommonStatusEnum.ENABLE.getStatus());
        List<DeliveryPickUpStoreSimpleRespVO> data = BeanUtils.toBean(list, DeliveryPickUpStoreSimpleRespVO.class);
        data.forEach(it -> RegionUtils.autoAssignCounty(RegionDataSet.ONLY_LATEST, it.getCountyCode(), it));
        return success(data);
    }

    @GetMapping("/list")
    @Operation(summary = "获得自提门店列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('trade:delivery:pick-up-store:query')")
    public CommonResult<List<DeliveryPickUpStoreRespVO>> getDeliveryPickUpStoreList(@RequestParam("ids") Collection<Long> ids) {
        List<DeliveryPickUpStoreDO> list = deliveryPickUpStoreService.getDeliveryPickUpStoreList(ids);
        if (CollUtil.isEmpty(list)) {
            return success(Collections.emptyList());
        }
        List<DeliveryPickUpStoreRespVO> data = BeanUtils.toBean(list, DeliveryPickUpStoreRespVO.class);
        data.forEach(vo -> RegionUtils.autoAssignCounty(RegionDataSet.ONLY_LATEST, vo.getCountyCode(), vo));
        return success(data);
    }

    @GetMapping("/page")
    @Operation(summary = "获得自提门店分页")
    @PreAuthorize("@ss.hasPermission('trade:delivery:pick-up-store:query')")
    public CommonResult<PageResult<DeliveryPickUpStoreRespVO>> getDeliveryPickUpStorePage(@Valid DeliveryPickUpStorePageReqVO pageVO) {
        PageResult<DeliveryPickUpStoreDO> pageResult = deliveryPickUpStoreService.getDeliveryPickUpStorePage(pageVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }
        PageResult<DeliveryPickUpStoreRespVO> data = BeanUtils.toBean(pageResult, DeliveryPickUpStoreRespVO.class);
        data.getList().forEach(vo -> RegionUtils.autoAssignCounty(RegionDataSet.ONLY_LATEST, vo.getCountyCode(), vo));
        return success(data);
    }

    @PostMapping("/bind")
    @Operation(summary = "绑定自提店员")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> bindDeliveryPickUpStore(@Valid @RequestBody DeliveryPickUpBindReqVO bindReqVO) {
        deliveryPickUpStoreService.bindDeliveryPickUpStore(bindReqVO);
        return success(true);
    }

}
